/* ==============================================================================
 * 创 建 者：lyt
 * 创建日期：2017-07-14	
 * 功能描述：

 * 修 改 人:
 * 修改時間:
 * 修改描述：
 * ==============================================================================*/
//会员信息获取
var getMemberViewUrl = baseUrl + 'MemberF/MemberView';
//会员信息编辑
var redactMemberUrl = baseUrl + 'MemberF/EditMemberView';
//获取银行列表
var getBankListUrl = baseUrl + 'common/GetBankList';
//修改頭像
var getSaveHeadIconUrl = baseUrl + "MemberF/SaveHeadIcon";
//修改昵稱
var getSaveNiNameUrl = baseUrl + "MemberF/SaveNiName";
// 修改地址
var SaveWalletAddrUrl = baseUrl + "MemberF/SaveWalletAddr";
//换绑手机号
var SaveTelUrl = baseUrl +"MemberF/SaveTel";
//给旧手机发短信
var SendBindOldTelCodeUrl = baseUrl +"MemberF/SendBindOldTelCode";
//给新手机发短信
var SendBindNewTelCodeUrl = baseUrl +"MemberF/SendBindNewTelCode";

var vu;
$(function() {
	initVue();
});

function initVue() {
	vu = new Vue({
		el: '#content',
		data: {
			member: '',
			banks: [],
			count: 0,
			NewTel:"",
			OldVerificationCode:'',
			NewVerificationCode:"",
			validCode:true,
			validCode1:true,
		},
		mounted: function() {
			this.getMemberView();
			//			this.getBankList();
		},
		methods: {
			getCount: getCount,
			SaveNiName: SaveNiName,
			SaveHead: SaveHead,
			getMemberView: getMemberView,
			getBankList: getBankList,
			redactMember: redactMember,
			SaveWalletAddr:SaveWalletAddr,
			getCode : function(e) {
				var _this = this;
				var code = $(e.target);
				var time = 60;
				common.post(SendBindOldTelCodeUrl, {}, 'json', function(r) {
					if(r.ErrorCode == 0) {
						if(_this.validCode) {
							_this.validCode = false;
							if(r.Data.RemainingTime){
								time = r.Data.RemainingTime;
							}
							var t = setInterval(function() {
								time--;
								code.text(time + "秒");
								if(time == 0) {
									clearInterval(t);
									code.text("获取验证码");
									_this.validCode = true;
								}
							}, 1000)
						}
					} else {
						mui.toast(r.ErrorMsg);
					}
				})
			},
			getCode1 : function(e) {
				if(!this.NewTel){
					mui.toast("请输入新手机号");return;
				}
				var _this = this;
				var code = $(e.target);
				var time = 60;
				common.post(SendBindNewTelCodeUrl, {Tel:this.NewTel}, 'json', function(r) {
					if(r.ErrorCode == 0) {
						if(_this.validCode1) {
							if(r.Data.RemainingTime){
								time = r.Data.RemainingTime;
							}
							_this.validCode1 = false;
							var t = setInterval(function() {
								time--;
								code.text(time + "秒");
								if(time == 0) {
									clearInterval(t);
									code.text("获取验证码");
									_this.validCode1 = true;
								}
							}, 1000)
						}
					} else {
						mui.toast(r.ErrorMsg);
					}
				})
			},
		}
	})
}

function getCount() {
	vu.count = vu.member.NiName.length;
}

//换绑手机
function SaveTel() {
	if (!vu.member.WalletAddr) {
		mui.toast("请输入钱包地址");
		return;	
	}
	common.post(SaveTelUrl, {
		NewTel: vu.NewTel,
		OldVerificationCode:vu.OldVerificationCode,
		NewVerificationCode:vu.NewVerificationCode,
	}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			mui.toast(r.ErrorMsg);
			setTimeout(function() {
				location.href = "memberCenter.html";
			}, 300)
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

//修改钱包
function SaveWalletAddr() {
	if (!vu.member.WalletAddr) {
		mui.toast("请输入钱包地址");
		return;	
	}
	common.post(SaveWalletAddrUrl, {
		WalletAddr: vu.member.WalletAddr
	}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			mui.toast(r.ErrorMsg);
			setTimeout(function() {
				location.href = "memberCenter.html";
			}, 300)
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

//修改昵稱
function SaveNiName() {
	if (!vu.member.NiName) {
		mui.toast("请输入昵称");
		return;
	}
	common.post(getSaveNiNameUrl, {
		NiName: vu.member.NiName
	}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			mui.toast(r.ErrorMsg);
			setTimeout(function() {
				location.href = "memberCenter.html";
			}, 300)
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}


// 设置头像
function SaveHead() {
	common.post(getSaveHeadIconUrl, {
		HeadIcon: vu.member.HeadIcon
	}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			mui.toast(r.ErrorMsg);
			location.reload();
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

function redactMember() {
	if (!vu.member.Tel) {
		mui.toast("請輸入手機號");
		return;
	}
	if (!vu.member.IdentityCard) {
		mui.toast("請輸入身份證號");
		return;
	}
	if (!vu.member.BankName) {
		mui.toast("請選擇银行");
		return;
	}
	if (!vu.member.SubBranchBank) {
		mui.toast("請輸入开户支行");
		return;
	}
	if (!vu.member.CardName) {
		mui.toast("請輸入开户姓名");
		return;
	}
	if (!vu.member.CardNumber) {
		mui.toast("請輸入银行卡號");
		return;
	}
	var data = {
		//		Name  : vu.member.Name ,
		NiName: vu.member.NiName,
		IdentityCard: vu.member.IdentityCard,
		BankName: vu.member.BankName,
		SubBranchBank: vu.member.SubBranchBank,
		CardName: vu.member.CardName,
		CardNumber: vu.member.CardNumber,
		//		RecommendName : vu.member.RecommendName,
		//		ParentName : vu.member.ParentName,
		Tel: vu.member.Tel,
	}
	common.post(redactMemberUrl, data, 'json', function(r) {
		if (r.ErrorCode == 0) {
			mui.toast(r.ErrorMsg);
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

function getBankList() {
	common.post(getBankListUrl, {}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			vu.banks = r.Data.List;
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

function getMemberView() {
	common.get(getMemberViewUrl, {}, 'json', function(r) {
		if (r.ErrorCode == 0) {
			vu.member = r.Data.member;
			getCount();
		} else {
			mui.toast(r.ErrorMsg);
		}
	})
}

//上傳图片
function doUpload1() {

	var formData = new FormData();
	if ($(".upload1")[0].files.length == 0) {
		mui.toast("请选择图片！");
		return false;
	};
	formData.append("file", $(".upload1")[0].files[0]);
	formData.append("picTypeName", "USER_HEAD");
	$.ajax({
		url: baseUrl + "common/uploadimg",
		type: "POST",
		data: formData,
		async: false,
		cache: false,
		contentType: false,
		processData: false,
		success: function(r) {
			r = JSON.parse(r);
			var u = r.Data.url;
			if (r.ErrorCode == 0) {
				//				mui.toast("上傳成功！");
				vu.member.HeadIcon = u;
				SaveHead();
			} else {
				mui.toast(r.ErrorMsg);
			}
		},
		error: function(returndata) {
			mui.toast("操作失败");
		}
	});
};

//上傳图片
function doUpload2() {
	var formData = new FormData();
	if ($(".upload2")[0].files.length == 0) {
		mui.toast("请选择图片！");
		return false;
	};
	formData.append("file", $(".upload2")[0].files[0]);
	formData.append("picTypeName", "USER_HEAD");
	$.ajax({
		url: baseUrl + "common/uploadimg",
		type: "POST",
		data: formData,
		async: false,
		cache: false,
		contentType: false,
		processData: false,
		success: function(r) {
			r = JSON.parse(r);
			var u = r.Data.url;
			if (r.ErrorCode == 0) {
				mui.toast("上傳成功！");
				vu.member.QrCodeUrl = u;
			} else {
				mui.toast(r.ErrorMsg);
			}
		},
		error: function(returndata) {
			mui.toast("操作失败");
		}
	});
};
